<?php

// +----------------------------------------------------------------------
// | SinianAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2019 祀念网络科技有限公司 [ http://www.sinianwork.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://demo.sinianwork.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：
// | github 代码仓库：
// +----------------------------------------------------------------------

namespace app\api\controller;
use app\data\model\ControlPlanModel;
use app\data\model\ControlPlanItemModel;
use think\facade\Db;
use think\facade\Request;
use think\admin\extend\CodeExtend;
use think\admin\Controller;


require __DIR__.'/../../../vendor/autoload.php';

use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Sms\V20210111\SmsClient;
use TencentCloud\Sms\V20210111\Models\SendSmsRequest;

/**
 * 腾讯云短信接口（定时任务）
 * Class index
 * @package app\index\controller
 */
class Sendmessage extends Controller
{

    /**
     * 计划超时审批发送短信提醒
     */
    public function PlanSend(){
        // $name = $this->Sendmsg(['J88888888888'],['17703406818']);
        // dump(json_decode($name,true));die;
        //计划列表
        $sendTime = date('Y-m-d H:i:s',time() - 200);
        $where[] = array('status','=',0);
        $where[] = array('deleted','=',0);
        $planList = ControlPlanModel::where($where)->where('create_at','<',$sendTime)->order('id desc')->column('create_at,plan_time,admin_id','id');
        $time = time();
        $sendNum = 0;
        $jsons = array();
        $ControlPlanItemModel = new ControlPlanItemModel();
        foreach ($planList as $k=>$v){
            $map = array('collect_id'=>$k,'status'=>0);
            $itmes = $ControlPlanItemModel->where($map)->where('create_at','<',$sendTime)->order('admin_authorize asc')->find()->toArray();
            if($itmes){
                $phone = array(Db::name('SystemUser')->where('id',$itmes['admin_id'])->value('contact_phone'));
                $data = array('计划单'.'（'.$itmes['collect_id'].'）');
                $jsons[] = $this->SendMsg($data,$phone);
                $sendNum++;
            }
            // if($itmes['admin_authorize'] == 2){
            //     if($time - $v['plan_time'] >= 7200){
            //         array_push($userId,$v['admin_id']);
            //     }
            // }else{
                
            // }
        }
        //判断差集
        // $diff = array_diff($data,array_keys($planList));
        
        // $res = json_decode($this->SendMsg($data,$phone),true);
        // $ret = 0;
        // $res['SendStatusSet'] ?? $ret = count($res['SendStatusSet']);
        // $create_at = array_column($planList,'create_at');
        $json['sms_num'] = $sendNum;
        $json['json_str'] = $jsons;
        $this->success('ok',$json,1);

    }
    
    
    
    public function SendMsg($data,$phone){
        try {
            // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
            // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
            $cred = new Credential("AKIDoGZedOYVKxLWUFzXxEJ9nCjEbmuwmPmC", "JQHvLXS8bjfOr54tfJpDOL5dgV9kALLC");
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            $httpProfile = new HttpProfile();
            $httpProfile->setEndpoint("sms.tencentcloudapi.com");
        
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            $clientProfile = new ClientProfile();
            $clientProfile->setHttpProfile($httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            $client = new SmsClient($cred, "ap-guangzhou", $clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            $req = new SendSmsRequest();
            $params = array(
                "PhoneNumberSet" => $phone,
                "SmsSdkAppId" => "1400774100",
                "TemplateId" => "1627146",
                "SignName" => "赋业科技",
                "TemplateParamSet" => $data
            );
            $req->fromJsonString(json_encode($params));
        
            // 返回的resp是一个SendSmsResponse的实例，与请求对象对应
            $resp = $client->SendSms($req);
        
            // 输出json格式的字符串回包
            return $resp->toJsonString();
        }
        catch(TencentCloudSDKException $e) {
            echo $e;
        }
    }
    
    
    
    
    
    
    
    
    
}